package com.bzb.secure.store.plugin;

import java.io.ByteArrayOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

public class RSAExample {
    private static final String TRANSFORMATION = "RSA/ECB/PKCS1Padding";
    private static final int KEY_SIZE = 1024;
    private static final int MAX_ENCRYPT_BLOCK = 117; // 根据秘钥长度计算得出
    private static final int MAX_DECRYPT_BLOCK = 128; // 根据秘钥长度计算得出

    public static KeyPair generateKeyPair() throws Exception {
      /*  KeyPairGenerator keyPairGenerator =
                KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "RSA");
        KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder("", KeyProperties.PURPOSE_DECRYPT *//*此参数只针对私钥设置*//*)
                .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                .setKeySize(KEY_SIZE)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                .build();
        keyPairGenerator.initialize(spec);*/

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(KEY_SIZE);
        return keyPairGenerator.generateKeyPair();
    }

    public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        int inputLength = data.length;
        int offSet = 0;
        byte[] cache;
        int i = 0;

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while (inputLength - offSet > 0) {
            if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLength - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        return out.toByteArray();
    }

    public static byte[] decrypt(byte[] encryptedData, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        int inputLength = encryptedData.length;
        int offSet = 0;
        byte[] cache;
        int i = 0;

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while (inputLength - offSet > 0) {
            if (inputLength - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLength - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        return out.toByteArray();
    }

    public static void main() {
//    public static void main(String[] args) {
        try {
            // 生成密钥对
            KeyPair keyPair = generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();

            // 原始数据
            String data = "3082015E041011223344556677881122334455667788041011223344556677881122334455667788043411223344551122334455112233445566778811223344556677881122334411223344556677881122334411223344556677881122048201008BE571B4F98FB2EE2A21A5420B7C4531AD3A0786027E394CEEED466EB047E0A3421879A03CD8282AE5DD4B38E3A9015B90A847420B0CADC74337B0197DEAD83667CF02569AB80F1058A06372BF6DCC584D21552855249980EE8071E571ECD8EEFF4EDC776A1C2691D53192FDB4EB8BBC263412E78DBF7E61178B9B346BB1A47336D487B59EF1D3A814D8857BD2C83ACFC1BF9093338789C8423D4D7723FE46BF1E32C829FB8A10718FAB5A0CD351F413A05F2571B12088554A9284D7BCDE415EE369C31D183D91901182415BA519D6C7DDE4E23262DC4C3D53325FB0C3AE3CD4D9A00CFC7DA72BAD4D46113087D0E505766A806C424D3B6E1A2ADA9888A83AD4";
            byte[] originalData = data.getBytes();

            // 加密
            byte[] encryptedData = encrypt(originalData, publicKey);

            // 解密
            byte[] decryptedData = decrypt(encryptedData, privateKey);

            // 输出结果
            System.out.println("原始数据：" + data);
            System.out.println("加密后的数据：" + new String(encryptedData));
            System.out.println("解密后的数据：" + new String(decryptedData));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}